package br.com.moove.bananasfit.view;

import org.json.JSONArray;
import org.json.JSONObject;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.RetryPolicy;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;

import br.com.moove.bananasfit.R;
import br.com.moove.bananasfit.R.id;
import br.com.moove.bananasfit.R.layout;
import br.com.moove.bananasfit.R.menu;
import br.com.moove.bananasfit.control.VolleySingleton;
import br.com.moove.bananasfit.model.PessoaFisica;
import br.com.moove.bananasfit.model.PessoaJuridica;
import br.com.moove.bananasfit.model.Servico;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Build;

public class DetalhesPJuridicaActivity extends ActionBarActivity implements
		Response.Listener<JSONObject>, Response.ErrorListener {

	
	PessoaFisica usuario;
	
	PessoaJuridica pessoaJuridica;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
		setContentView(R.layout.detalhe_pessoa_juridica_activity);

		pessoaJuridica = (PessoaJuridica) getIntent()
				.getSerializableExtra("pessoaJuridica");
		
		usuario = (PessoaFisica) getIntent().getSerializableExtra(
				"Usuario");

		JSONObject json = new JSONObject();
		try{
		json.put("ChavePessoaFisica", usuario.getId());
		json.put("ChavePessoaJuridica", pessoaJuridica.getId());
		RequestQueue queue = Volley.newRequestQueue(this);
		JsonObjectRequest jsObjRequest = new JsonObjectRequest(
				Request.Method.POST,
				"http://www.bananasfit.com.br/api/usuarioapi/detalharPessoaJuridica",
				json, this, this);
		Log.d("Detalhe PJuridica:", json.toString());
		int socketTimeout = 30000;// 30 seconds - change to what you
		// want
		RetryPolicy policy = new DefaultRetryPolicy(socketTimeout,
		DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
		DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
		jsObjRequest.setRetryPolicy(policy);
		queue.add(jsObjRequest);
		setProgressBarIndeterminateVisibility(true);
		}catch (Exception ex){
			Log.d("Erro", ex.getMessage());
		}
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.detalhes_pjuridica, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onErrorResponse(VolleyError error) {
		// TODO Auto-generated method stub
		Log.d("Error: ", error.toString());
	}

	@Override
	public void onResponse(JSONObject response) {
		// TODO Auto-generated method stub
		Log.d("JSON:", response.toString());
		pessoaJuridica.carregarJsonPessoaJuridica(response);
		DetalhesPessoaJuridicaFragment d = DetalhesPessoaJuridicaFragment
				.getInstance(pessoaJuridica, usuario);
		setSupportProgressBarIndeterminateVisibility(false);
		getFragmentManager().beginTransaction().replace(R.id.detail, d)
				.commit();
		
	}	

}
